//
//  AppDelegate.m
//  UITextField_UIButton
//
//  Created by 郑天宇 on 15/9/29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
#pragma mark - 知识点1 UITextField
    /* 1.创建对象 */
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
    
    /* center属性,中心点 */
    textField.center = CGPointMake(375/2, 100);
    [self.window addSubview:textField];
    [textField release];
    textField.tag = 100;
    textField.backgroundColor = [UIColor whiteColor];
    
    textField.textColor = [UIColor blackColor];
    
    /* 核心的API */
    /* 占位符 */
    textField.placeholder = @"请输入用户名";
    /* 边框风格 */
    textField.borderStyle = 3;
    
    /* 弹出的键盘类型 */
    textField.keyboardType = 0;
    
    /* 密文输入 */
    textField.secureTextEntry = YES;
    
    /* 键盘上的返回键类型 */
    textField.returnKeyType = UIReturnKeyGo;
    
    /* 键盘出现 就一个黑的一个白的 */
    textField.keyboardAppearance = UIKeyboardAppearanceDark;
    
    /* 清除按键模式 */
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
#pragma mark - 知识点 系统类的delegate使用
    /* 1.签协议
       2.指定 代理人
       3.实现相关协议方法*/
    
    textField.delegate = self;
   
#pragma mark - 知识点 UIButton
    /* 1.创建对象 */
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(50, 200, 100, 100);

    [self.window addSubview:button1];

    
    button1.backgroundColor = [UIColor yellowColor];
    
    /* 2.核心API 
       button的标题*/
    [button1 setTitle:@"Next" forState:UIControlStateNormal];
    
    /* button的字体颜色 */
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    /* button字体大小 */
    button1.titleLabel.font = [UIFont systemFontOfSize:30];
    
    /* button 添加点击事件 */
    [button1 addTarget:self action:@selector(buttonClicked:) /* 注：buttonCliked方法还没有实现*/forControlEvents:UIControlEventTouchUpInside];
    
    
    
    /* 另一个button,定制类型 */
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(200, 200, 100, 100);
    [self.window addSubview:button2];
    button2.backgroundColor = [UIColor cyanColor];
    [button2 setTitle:@"Custom" forState:UIControlStateNormal];
    
    /* 设置button的图片 */
    [button2 setBackgroundImage:[UIImage imageNamed:@"屏幕快照 2015-09-29 下午2.41.10.png"] forState:UIControlStateNormal];

    /* 设置button的图片 */
    //[button2 setImage:[UIImage imageNamed:@"/Users/dllo/Desktop/工程/UI/课堂代码/UITextField_UIButton/iconfont-chunvzuo.png"] forState:UIControlStateNormal];
    
    /* 设置title位置 */
    button2.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    /* 设置前景图的位置 */
    //button2.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    /* 第三个button */
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(50, 350, 100, 100);
    button3.backgroundColor = [UIColor orangeColor];
    [self.window addSubview:button3];
    
    [button3 setTitle:@"ON" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(button3:) forControlEvents:UIControlEventTouchUpInside];
    [button3 setTitle:@"Long" forState:UIControlStateHighlighted];
    button3.selected = YES;
    
    
    [_window release];
    return YES;
}

- (void)button3:(UIButton *)button
{
#if 0    // 方法一
    if ([button.currentTitle isEqualToString:@"ON"] /*当前的title*/) {
        [button setTitle:@"OFF" forState:UIControlStateNormal];
    } else if ([button.currentTitle isEqualToString:@"OFF"]) {
        [button setTitle:@"ON" forState:UIControlStateNormal];
        
    }
#endif
    
#if 1   // 方法二
    button.selected = !button.selected;
    if (button.selected == YES) {
        [button setTitle:@"ON" forState:UIControlStateNormal];
    } else [button setTitle:@"OFF" forState:UIControlStateNormal];

#endif
    

    
}

#pragma mark - 实现button点击方法
-(void)buttonClicked:(UIButton *)button {
    NSLog(@"DDD");
    UITextField *findTF = (UITextField *)[self.window viewWithTag:100];
    [findTF resignFirstResponder];
}


#pragma mark - UITextField协议方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"！！！%s", __func__);
    
    return YES;
}

/* 当点击键盘上的return按键时,调用此方法 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%s", __func__);
    /* 点击return按键 键盘落下 */
    [textField resignFirstResponder]; /* 放弃第一响应,即回收键盘 */
    return YES;
}

/* 不允许输入'a' */
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"%ld, %ld", range.location, range.length);
    
    /* 通过函数将结构体转换为字符串对象 */
    NSLog(@"%@", NSStringFromRange(range));
    NSLog(@"%@", string);
    
    if ([string isEqualToString:@"a"] || range.location > 10) {
        return NO;
    } else {
        return YES;
    }
    return YES;
}



- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
